<?php
# ***** BEGIN LICENSE BLOCK *****
# This file is part of "myWiWall".
# Copyright (c) 2007-2008 CRP Henri Tudor and contributors.
# All rights reserved.
#
# "myWiWall" is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# 
# "myWiWall" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with "myWiWall"; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# ***** END LICENSE BLOCK *****
require_once (dirname(__FILE__) . '/../includes/util/Util.class.php');
require_once (dirname(__FILE__) . '/../includes/install/InstallException.class.php');
require_once (dirname(__FILE__) . '/../includes/install/ConfigInstallException.class.php');
require_once (dirname(__FILE__) . '/../includes/install/DatabaseInstallException.class.php');
require_once (dirname(__FILE__) . '/../includes/install/Install.class.php');
require_once (dirname(__FILE__) . '/../includes/clearbricks/common/lib.l10n.php');

if (isset($_POST['validate_install']))
{
	// ActionState business.
	$logUsers 			= (isset($_POST['logs_users_activate'])) ? 'true' : 'false';
	$useOpenID 			= (isset($_POST['openid_mode'])) ? 'true' : 'false';
	$useServiceBrowser 	= (isset($_POST['service_browser_activate'])) ? 'true' : 'false';
	$errorMessage		= '';
	
	try
	{
		$configFileIn = dirname(__FILE__) . '/../includes/config-sample.php';
		$configFileOut = dirname(__FILE__) . '/../includes/config.php';
		
		$params = array('database_login' 			=> $_POST['database_login'],
						'database_password' 		=> $_POST['database_password'],
						'database_name' 			=> $_POST['database_name'],
						'database_url' 				=> $_POST['database_url'],
						'theme_name' 				=> $_POST['theme_name'],
						'language' 					=> $_POST['language'],
						'openid_mode' 				=> $useOpenID,
						'application_name' 			=> $_POST['application_name'],
						'logs_users_activate' 		=> $logUsers,
						'logs_users_path' 			=> $_POST['logs_users_path'],
						'service_browser_activate' 	=> $useServiceBrowser,
						'service_browser_uri'		=> $_POST['service_browser_uri'],
						'portal_uri' 				=> $_POST['portal_uri']);
		
		$installer = new Install($configFileIn, $configFileOut, $params);
		$installer->install();
		
		header('Location: success.php');
		exit;
	}
	catch (DatabaseInstallException $dbEx)
	{
		$dbmsError = $dbEx->getDBMSMessage();
		
		switch ($dbEx->getCode())
		{
			case DatabaseInstallException::FAILED_TO_CONNECT:
				$errorMessage = __("Failed to connect to the Database Management System. Please verify the provided database configuration.");
			break;
			
			case DatabaseInstallException::FAILED_TO_CREATE_DB:
				$errorMessage = __("Failed to create database: ${dbmsError}. Is the chosen database name already used ?");
			break;
			
			case DatabaseInstallException::FAILED_TO_CREATE_TABLE:
				$errorMessage = __("Failed to create a table in the database: ${dbmsError}.");
			break;
			
			case DatabaseInstallException::FAILED_TO_INSERT_DATA:
				$errorMessage = __("Failed to insert data in the database: ${dbmsError}.");
			break;
		}
	}
	catch (InstallException $installEx)
	{
		switch ($installEx->getCode())
		{
			case InstallException::CONFIG_FILE_ALREADY_GENERATED:
				$errorMessage = __("Installation already done. If you really want to reconfigure the application, please edit the '/includes/config.php' file manually");
			break;
			
			case InstallException::CONFIG_FOLDER_NOT_WRITABLE:
				$errorMessage = __("The configuration folder ('/includes') is not writable by the web server.");
			break;
			
			case InstallException::CONFIG_SAMPLE_NOT_READABLE:
				$errorMessage = __("The template configuration file ('/includes/config-sample.php') is not readable by the web server.");
			break;
			
			case InstallException::MISSING_MYSQL_FUNCTIONS:
				$errorMessage = __("The application needs mysql functions to be run");
			break;
			
			case InstallException::WIDGETS_FOLDER_NOT_WRITABLE:
				$errorMessage = __("The '/widgets' folder is not writable");
			break;
		}
	}
	
	// I we are here, we are sure an exception was thrown by Install::install, and catched by this script.
	$availableLanguages = Util::getDirectoryContentNames(dirname(__FILE__) . '/../locales/', array('.svn'));
	$availableThemes = Util::getDirectoryContentNames(dirname(__FILE__) . '/../views', array('common', '.svn'));
	$comingAgain = true;
	
	require_once (dirname(__FILE__) . '/../views/common/install/install.tpl');
}
else
{
	// ViewState business.
	$availableLanguages = Util::getDirectoryContentNames(dirname(__FILE__) . '/../locales/', array('.svn'));
	$availableThemes = Util::getDirectoryContentNames(dirname(__FILE__) . '/../views', array('common', '.svn'));
	$comingAgain = false;
	
	// View inclusion.
	require_once (dirname(__FILE__) . '/../views/common/install/install.tpl');
}
?>
